package com.lgcns.user;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.security.access.prepost.PreAuthorize;

@Service
public class UserService
{
	
	@Autowired
	private UserDao userDao;
	
	public void setUserDao(UserDao userDao)
	{
		this.userDao = userDao;
	}
	
	public int insert(User user)
	{
		return userDao.insert(user);
	}
	
	public User login(User user)
	{
		return userDao.login(user);
	}

	
	public void insertAuthority(String email, String authority)
	{
		userDao.insertAuthority(email, authority);
	}
	
	@PreAuthorize("#user.email == principal.username or hasRole('ROLE_ADMIN')")
	public int modify(User user)
	{
		return userDao.modify(user);
	}
	
	@PreAuthorize("#user.email == principal.username or hasRole('ROLE_ADMIN')")
	public void changePasswd(User user)
	{
		userDao.changePasswd(user);
	}
	
	@PreAuthorize("#user.email == principal.username or hasRole('ROLE_ADMIN')")
	public void bye(User user)
	{
		userDao.bye(user);
	}
	
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	public ArrayList<User> getAllUsers()
	{
		return userDao.getAllUsers();
	}
	
	@PreAuthorize("#email == principal.username or hasRole('ROLE_ADMIN')")
	public User getUserByEmail(String email)
	{
		return userDao.getUserByEmail(email);
	}
	
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	public ArrayList<User> getUsersByKeyword(String keyword)
	{
		return userDao.getUsersByKeyword(keyword);
	}
}